package com.sc.nft.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.Privilege;
import com.sc.nft.entity.dto.SavePrivilegeDTO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.PrivilegeService;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "特权")
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/admin/privilege")
public class PrivilegeController {
    private final PrivilegeService privilegeService;


    @PostMapping(value = "/getPrivilegeList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取配置特权列表", notes = "获取配置特权列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result getPrivilegeList() {
        List<Privilege> list = (List<Privilege>) new Privilege().selectAll();
        list.forEach(privilege -> {
            privilege.setTransferDiscount(privilege.getTransferDiscount().multiply(Convert.toBigDecimal(100)));
            privilege.setConsignmentSalesDiscount(privilege.getConsignmentSalesDiscount().multiply(Convert.toBigDecimal(100)));
            privilege.setWithdrawServerChargeDiscount(privilege.getWithdrawServerChargeDiscount().multiply(Convert.toBigDecimal(100)));
        });
        return Result.success(list);
    }

    @PostMapping(value = "/savePrivilegeById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改配置列表", notes = "修改配置列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result savePrivilegeById(@Valid @RequestBody List<SavePrivilegeDTO> savePrivilegeDTOS) {
       savePrivilegeDTOS.forEach(savePrivilegeDTO -> {
           if (BeanUtil.isEmpty(savePrivilegeDTO) && savePrivilegeDTO.getId() != null && (savePrivilegeDTO.getTransferDiscount() != null || savePrivilegeDTO.getConsignmentSalesDiscount() != null || savePrivilegeDTO.getWithdrawServerChargeDiscount() != null)) {
               throw new GlobalRunTimeException("参数错误");
           }
           UpdateWrapper<Privilege>updateWrapper=new UpdateWrapper<>();
           updateWrapper.eq("id",savePrivilegeDTO.getId());
           if (savePrivilegeDTO.getTransferDiscount() != null) {
               updateWrapper.set("transfer_discount",savePrivilegeDTO.getTransferDiscount().divide(Convert.toBigDecimal(100)));
           }
           if (savePrivilegeDTO.getWithdrawServerChargeDiscount() != null) {
               updateWrapper.set("withdraw_server_charge_discount",savePrivilegeDTO.getWithdrawServerChargeDiscount().divide(Convert.toBigDecimal(100)));
           }
           if (savePrivilegeDTO.getConsignmentSalesDiscount() != null) {
               updateWrapper.set("consignment_sales_discount",savePrivilegeDTO.getConsignmentSalesDiscount().divide(Convert.toBigDecimal(100)));
           }
           new Privilege().update(updateWrapper);
       });
        return Result.success();
    }
}
